package debtbundler.presentation.balancesheeteditor;

import java.awt.Dimension;
import java.text.DateFormat;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import debtbundler.domain.entity.BalanceSheet;
import debtbundler.domain.entity.Signature;
import debtbundler.presentation.basecomponents.DomainViewComponent;

/**
 * Displays the signature date and the signer's name of a balance sheet in a
 * vertical form layout.
 *  
 * @author Anssi
 *
 */
public class SignaturePanel extends JPanel implements DomainViewComponent<BalanceSheet> {
	private static final long serialVersionUID = 1L;
	
	protected BalanceSheet balanceSheet;
	protected JLabel signedDateFieldLabel;
	protected JFormattedTextField signedDateField;
	protected JLabel signedByFieldLabel;
	protected JTextField signedByField;
	
	/**
	 * Creates a new signature box for given balance sheet.
	 * 
	 * @param balanceSheet	balance sheet whose signature to view (not null)
	 */
	public SignaturePanel(BalanceSheet balanceSheet) {	
		super();
		
		this.balanceSheet = balanceSheet;
		
		signedDateField = new JFormattedTextField(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT));
		signedDateField.setEditable(false);
		signedDateField.setMaximumSize( new Dimension(signedDateField.getMaximumSize().width, signedDateField.getPreferredSize().height) );
		signedDateFieldLabel = new JLabel("Kuitattu:");
		signedDateFieldLabel.setLabelFor(signedDateField);
		
		signedByField = new JTextField();
		signedByField.setEditable(false);
		signedByField.setMaximumSize( new Dimension(signedByField.getMaximumSize().width, signedByField.getPreferredSize().height) );
		signedByFieldLabel = new JLabel("Kuittaaja:");
		signedByFieldLabel.setLabelFor(signedByField);
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		Box signedDateBox = Box.createHorizontalBox();
		signedDateBox.add(signedDateFieldLabel);
		signedDateBox.add(Box.createRigidArea(new Dimension(18,0)));
		signedDateBox.add(signedDateField);
		signedDateBox.add(Box.createHorizontalGlue());
		add(signedDateBox);
		
		add(Box.createRigidArea(new Dimension(0,6)));
		
		Box signedByBox = Box.createHorizontalBox();
		signedByBox.add(signedByFieldLabel);
		signedByBox.add(Box.createRigidArea(new Dimension(12,0)));
		signedByBox.add(signedByField);
		signedByBox.add(Box.createHorizontalGlue());
		add(signedByBox);
	}
	
	
	@Override
	public BalanceSheet getDomainComponent() {
		return balanceSheet;
	}

	@Override
	public void refreshDomainFields() {
		Signature signature = balanceSheet.getSignature();
		if (signature != null) {
			signedDateField.setValue(signature.getCreatedDate().getTime());
			signedByField.setText(signature.getCreatedBy());
		} else {
			signedDateField.setValue(null);
			signedByField.setText("");
		}
	}

}
